/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */

package cn.knet.sp.ncrm.common.persistence;

import java.util.HashMap;
import java.util.Map;

/**
 * User: zhangbin CreateDate: 2015/7/8 11:34
 */
public class AjaxMessage extends HashMap<String, Object> {

	private AjaxMessage() {

	}

	public static AjaxMessage error() {
		return error("失败");
	}

	public static AjaxMessage error(String message) {
		AjaxMessage ajaxMessage = new AjaxMessage();
		ajaxMessage.setState(false);
		ajaxMessage.setMessage(message);
		return ajaxMessage;
	}

	public static AjaxMessage success() {
		return success("成功");
	}

	public static AjaxMessage success(String message) {
		AjaxMessage ajaxMessage = new AjaxMessage();
		ajaxMessage.setState(true);
		ajaxMessage.setMessage(message);
		return ajaxMessage;
	}


	@Override
	public Object put(String key, Object value) {
		if ("message".equals(key) || "state".equals(key)) {
			throw new UnsupportedOperationException("this tool unsupport key like" + key);
		} else {
			return super.put(key, value);
		}
	}

	@Override
	public void putAll(Map<? extends String, ?> m) {
		throw new UnsupportedOperationException("this tool unsupport putAll!");
	}

	public void setState(boolean state){
		super.put("state", state);
	}

	public void setMessage(String message) {
		super.put("message", message);
	}

	public void appendMessage(String message) {
		StringBuffer bf = new StringBuffer(this.get("message").toString());
		bf.append(message);
		super.put("message", bf.toString());
	}

	public boolean isSuccess() {
		return (Boolean)this.get("state");
	}

	public void makeError(String message) {
		this.setState(false);
		this.setMessage(message);
	}

}
